function isWeiXin() {
    var ua = window.navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
        return true;
    } else {
        return false;
    }
}
/**获取路径参数 */
function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) { return pair[1]; }
    }
    return (false);
}

// 获取城市名
let citynm = decodeURIComponent(getQueryVariable("cityname"));
localStorage.setItem("cityname", citynm);
console.log(citynm)



if (isWeiXin()) {
    console.log(333)
} else {
    if (!localStorage.getItem("m_token")) {
        $.ajax({
            type: "GET",
            url: `http://u.bendibao.com/?controller=fankui&action=createCustomerInfo`,
            dataType: "json",
            success: function (res) {
                if (res.code === 0) {
                    localStorage.setItem("m_token", res.data.token);
                }
            }
        });
    }
}
let reqLogin = null; // 登录轮询定时器

/**
 * 底部菜单导航
 */
$(".tab").click(function () {
    $(this).addClass("tab-select").siblings().removeClass("tab-select");
    $(this).find("img").eq(0).show().siblings().hide();
    $(this).siblings().find("img").eq(0).hide().siblings().show();
    let content = $(this).children("div").eq(1).text()
    if (content == "我要反馈") {
        $(".feed").show();
        $(".record").hide();
    } else {
        $(".feed").hide();
        $(".record").show();

        let path = localStorage.getItem("head_img")
        if (path) {
            $(".user").children("img").attr("src", path);
        }

        // 获取历史反馈
        let token = localStorage.getItem("m_token")
        $.ajax({
            type: "GET",
            url: `http://u.bendibao.com/?controller=fankui&action=getHistory&token=${token}`,
            dataType: "json",
            success: function (res) {
                if (res.code === 0) {
                    let record = res.data;
                    let count = record.length;
                    $(".feed-count").text(count);
                    $(".record-ul").html("");
                    record.forEach(el => {
                        $(".record-ul").prepend(`<div class="record-list">
                        <div class="desc-content">`+ el.feed_content + `</div>
                        <div class="desc-time">`+ el.add_time + `</div>
                        </div>`)
                    })
                }
            }
        });
    }
})

/**
 * 获取文本框的字数长度
 */
$(".desc").keyup(function () {
    let length = $(this).val().length
    $(this).siblings().text(`${length}/200`)
});

/**
 * 发送按钮事件
 */
$(".send").click(function () {
    let url = $(".url").val();
    let feedContent = $(".desc").val();
    let tel = $(".tel").val();
    let feedLth = feedContent.length
    if (feedContent == "" || tel == "") {
        $(".tips-dia").children(".tips-content").text("请补充反馈内容或联系方式后再提交")
        $(".mask").css("display", "flex");
        return
    } else if (feedLth < 10) {
        $(".tips-dia").children(".tips-content").text("反馈描述不得少于10个字")
        $(".mask").css("display", "flex");
        return
    }


    // 发送成功后
    let cityName = localStorage.getItem("cityname") || "长沙";
    let token = localStorage.getItem("m_token");
    let m = localStorage.getItem("m") ? "2" : "1";
    $.ajax({
        type: "POST",
        url: `http://u.bendibao.com/?controller=fankui&action=saveFeedContent&m=${m}`,
        data: {
            cityname: cityName,
            url: url,
            feed_content: feedContent,
            token: token,
            tel: tel
        },
        dataType: "json",
        success: function (res) {
            console.log(res)
            if (res.code === 0) {
                let m = localStorage.getItem("m")
                if (m == "1") {
                    $(".send-desc").text("感谢您的反馈");
                    $(".send-footer").hide();
                    $(".send-mask").css("display", "flex");
                    $(".send-success").show();
                    // setTimeout(() => {
                    //     $(".send-mask").hide();
                    //     $(".send-success").hide();
                    // }, 2000)
                } else {
                    $(".send-mask").css("display", "flex");
                    $(".qr-code").hide();
                    $(".wx-gzh").hide();
                    $(".send-title").text("成功提交");
                    $(".send-desc").text("马上开通提醒，有新回复第一时间通知你");
                    $(".send-close").text("取消")
                    $(".open-qr-code").show();
                    $(".send-success").show();
                }
                $(".tel").val("");
                $(".desc").val("");
                $(".url").val("");
            }
        }
    });





})

/**
 * 扫码关注事件
 */
function getFollow() {
    let token = localStorage.getItem("m_token");
    $.ajax({
        type: "GET",
        url: `http://u.bendibao.com/?controller=fankui&action=checkLogin&m=1&token=${token}`,
        dataType: "json",
        success: function (res) {
            console.log(res)
            if (res.code === 0) {
                let nowTime = new Date().getTime();
                localStorage.setItem("startTime", nowTime);
                clearInterval(reqLogin);
                $(".qr-code").hide();
                $(".send-title").text("成功关注");
                $(".send-desc").text("我们将在三个工作日内处理您的反馈");
                $(".send-close").text("好的")
                $(".open-qr-code").hide();
                $(".send-success").show();
                $(".qr-img").attr("src", "");
                localStorage.setItem("m", "1");
                let path = res.data.union_info.head_img_url;
                localStorage.setItem("head_img", path);
            }
        }
    });
}

/**
 * 关闭提醒弹出框
 */

$(".tips-btn").click(function () {
    $(".mask").hide();
})

/**
 * 关闭成功发送弹窗
 */

$(".send-close").click(function () {
    $(".send-mask").hide();
})

/**
 * 打开微信公众号二维码
 */

$(".open-qr-code").click(function () {
    if (isWeiXin()) {
        // -------------微信浏览器端
        $(".send-success").hide();
        $(".wx-gzh").css("display", "flex");
    } else {
        // -------------非微信浏览器端
        let token = localStorage.getItem("m_token");
        $(".qr-img").attr('src', "");
        $(".send-success").hide();
        $(".qr-code").css("display", "flex");
        // 获取二维码请求
        $.ajax({
            type: "GET",
            url: "http://u.bendibao.com/",
            dataType: "json",
            data: {
                controller: "fankui",
                action: "getQrCode",
                token,
            },
            success: function (res) {
                let path = res.data.qrCode; // 获取二维码路径
                $(".qr-img").attr('src', path);
                $(".loading").hide();
                $(".qr-img").show();


                reqLogin = setInterval(getFollow, 2000)
            }
        });
    }
})

/**
* 关闭微信公众号二维码
*/
$(".qr-close").click(function () {
    $(".send-mask").hide();
    clearInterval(reqLogin)
})

